package com.extrace.loader;

import android.app.Activity;
import android.util.Log;

import com.extrace.model.PackageRoute;
import com.extrace.net.HttpAsyncTask;
import com.extrace.net.HttpResponseParam;
import com.extrace.net.IDataAdapter;
import com.extrace.net.JsonUtils;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class PackageRouteLoader extends HttpAsyncTask {

    String url;
    private IDataAdapter<List<PackageRoute>> adapter;
    private Activity context;

    public PackageRouteLoader(IDataAdapter<List<PackageRoute>> adapter, Activity context) {
        super(context);
        this.adapter = adapter;
        this.context = context;

        url = "http://192.168.6.79:8080/TestCxfHibernate_war_exploded/REST/Domain/";
    }

    @Override
    public void onDataReceive(String class_name, String json_data) {
        if (JsonUtils.fromJson(json_data, new TypeToken<List<PackageRoute>>(){})!=null) {
            List<PackageRoute> list = JsonUtils.fromJson(json_data, new TypeToken<List<PackageRoute>>(){});
            adapter.setData(list);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onStatusNotify(HttpResponseParam.RETURN_STATUS status, String str_response) {
        Log.i("onStatusNotify", "onStatusNotify: " + str_response);
    }

    public void getPackageRoute(String exId) {
        url += "getPackageRoute/" + exId + "?_type=json";

        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
